import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Store from "../store";
Vue.use(VueRouter)

// VueRouter.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch(err =>err) 
// }
  

const routes = [
  {
    path:'/',
    redirect : '/home' //跳转至默认页面
  },
  {
    path: '/home',
    name: 'home',
    component: Home
  },
  {
    path :'/goods',
    name :'good',
    component: () => import(/* webpackChunkName: "goods" */ '../views/MoreCommodity.vue')

  },
  {
    path :'/user',
    name :'user',
    component: () => import(/* webpackChunkName: "user" */ '../views/User.vue')

  },
  {
    path :'/order',
    name :'order',
    component: () => import(/* webpackChunkName: "order" */ '../views/Order.vue')

  },
  {
    path :'/free',
    name :'free',
    component: () => import(/* webpackChunkName: "free" */ '../views/Free.vue')

  }
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 导航守卫
router.beforeEach((to,from,next)=>{
  if(to.path === '/user' || to.path === '/order' || to.path === '/free'){
    let token = localStorage.getItem('token')
    if(token){
      // 有的情况 进行一些关于token的验证
      next()
    }else {
      // 打开登录窗口
      Store.commit('changeLoginIsShow',true)
    }
    return
  }
  next()
})

export default router
